package cn.xsly.ymcc.web.controller;

import cn.xsly.ymcc.dto.CourseOrderVo;
import cn.xsly.ymcc.dto.CourseParamDto;
import cn.xsly.ymcc.dto.CourseStateVo;
import cn.xsly.ymcc.service.ICourseService;
import cn.xsly.ymcc.domain.Course;
import cn.xsly.ymcc.query.CourseQuery;
import cn.xsly.ymcc.result.JSONResult;
import cn.xsly.ymcc.result.PageList;
import cn.xsly.ymcc.vo.CourseDetailVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;
    //获取订单信息
    @GetMapping(value = "/info/{ids}")
    public JSONResult orderList(@PathVariable String ids){
        CourseOrderVo courseOrderVo = courseService.orderList(ids);
        return JSONResult.success(courseOrderVo);
    }

    //查看课程收费状态
    @GetMapping(value = "/courseState/{id}")
    public JSONResult courseStateById(@PathVariable Long id){
        CourseStateVo courseStateVo = courseService.courseStateById(id);
        return JSONResult.success(courseStateVo);
    }
    /*
    查看课程详情
   */
    @RequestMapping(value="/detail/data/{id}",method= RequestMethod.GET)
    public JSONResult courseDetailById(@PathVariable("id")  Long id){
        CourseDetailVo detailVo = courseService.courseDetailById(id);

        return JSONResult.success(detailVo);
    }

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult save(@RequestBody CourseParamDto dto) {
        courseService.save(dto);
        return JSONResult.success();
    }
    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/saveOrUpdate",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Course course){
        if(course.getId()!=null){
            courseService.updateById(course);
        }else{
            courseService.insert(course);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(courseService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query) {
        Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
        Wrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name", query.getKeyword());
        page = courseService.selectPage(page, wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(), page.getRecords()));
    }

    /**
     * 课程上线
     */
    @RequestMapping(value = "/onLineCourse/{id}", method = RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("id")Long id) {
        courseService.onLineCourse(id);
        return JSONResult.success();

    }
    /**
     * 课程下线
     */
    @RequestMapping(value = "/offLineCourse/{id}", method = RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("id")Long id) {
        courseService.offLineCourse(id);
        return JSONResult.success();

    }

}
